﻿using Ls.Core;
using Ls.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ls.Web.Api.Models
{

    /// <summary>
    /// 整车仓库库存表 记录仓库的库存情况服务
    /// </summary>
    public class WhInventoryService : BaseService
    {
        public WhInventoryService() : base("Default", "view_inventory", "实时库存查询")
        {
        }

        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="sqlList">执行代码数组</param>
        /// <returns></returns>
        public int ExecTranSql(List<dynamic> sqlList)
        {
            int result = 0;
            using (var context = this.db.UseTransaction(true))
            {
                try
                {
                    foreach (dynamic sql in sqlList)
                    {
                        IDictionary<string, object> parameters =null;
                        Data.IDbCommand Command = context.Sql(GetSqlStr(sql,ref parameters));
                        foreach (KeyValuePair<string, object> k in parameters) {
                            Command.Parameter(k.Key,k.Value);
                        }
                        result += Command.Execute();
                    }

                    if (result == sqlList.Count)
                    {
                        context.Commit();
                    }
                    else
                    {
                        throw (new Exception("提交发生异常"));
                    }
                }
                catch (Exception e)
                {
                    context.Rollback();
                    throw;
                }
            }
            return result;
        }

        /// <summary>
        /// sql类型的枚举
        /// </summary>
        public enum sqlType
        {
            delete = 0,
            insert = 1,
            update = 2
        };

        /// <summary>
        /// 将对象转换为sql语句，带参数
        /// </summary>
        /// <param name="sql">sql对象</param>
        /// <param name="parameters">返回参数集合（已经合并）</param>
        /// <returns></returns>
        public string GetSqlStr(dynamic sql,ref IDictionary<string, object> parameters)
        {
            string sqlStr = ""; 

            if (sql.sqlType == sqlType.insert)
            {
                IDictionary<string, object> parameter = GetColumns(sql.parameter);
                List<string> lstColumn = new List<string>();
                foreach (KeyValuePair<string, object> k in parameter)
                {
                    lstColumn.Add(k.Key); 
                }
                sqlStr = "insert into " + sql.tablename + "(" + string.Join(",", lstColumn) + ") values(@" + string.Join(", @", lstColumn) + ")";

                parameters = parameter;
            }
            else if (sql.sqlType == sqlType.update)
            {
                IDictionary<string, object> parameter = GetColumns(sql.parameter);
                IDictionary<string, object> where = GetColumns(sql.where);

                List<string> lstUpdate = new List<string>();
                List<string> lstWhere = new List<string>();
                foreach (KeyValuePair<string, object> k in parameter)
                {
                    lstUpdate.Add(k.Key + " = @" + k.Key); 
                }

                foreach (KeyValuePair<string, object> k in where)
                {
                    lstWhere.Add(k.Key + " = @" + k.Key); 

                    if (!parameter.ContainsKey(k.Key)) {
                        parameter.Add(k.Key, k.Value);
                    }
                }
                sqlStr = "update " + sql.tablename + " set " +  string.Join(",", lstUpdate) + " where " + string.Join(" and ", lstWhere);

                parameters = parameter;
            }
            else if (sql.sqlType == sqlType.delete)
            {
                IDictionary<string, object> parameter = GetColumns(sql.parameter);
                IDictionary<string, object> where = GetColumns(sql.where);

                List<string> lstWhere = new List<string>();
                foreach (KeyValuePair<string, object> k in where)
                {
                    lstWhere.Add(k.Key + " = @" + k.Key); 
                }
                sqlStr = "delete from " + sql.tablename + " where " + string.Join(" and ", lstWhere);

                parameters = parameter;
            }

            return sqlStr;
        }
    }
}
